package koalacam.server;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

/**
 * 门禁管理
 * @author Wangshutao
 * 2017年7月5日
 */
public class SysteM {
	
	/**
	 * 获取所有可用本地主机
	 * @param cookieStore
	 * @param url
	 * @throws Exception
	 */
	public static void boxes(CookieStore cookieStore,String url) throws Exception{
		System.out.println("Start GET /system/boxes	获取所有可用本地主机 ...");
		
		//自定义HttpClient 设置CookieStore
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        HttpGet request = new HttpGet(url);
        
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
        
        //解析JSON数据
        JSONObject resp = new JSONObject(responseBody);
        int result = resp.optInt("code", -1);
        if (result != 0) {
            System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
        }else{
        	//在线版 返回公司账号下绑定的所有主机
        	//离线版 如果是单机 只返回本机；如果是级联，则返回所有主机
        	JSONArray array = resp.getJSONArray("data");
        	for (int i = 0; i < array.length(); i++) {
				JSONObject box = (JSONObject)array.get(i);
				System.out.println("id:" + box.getInt("id") + " , box_address:" + box.getString("box_address") + " , box_token:" + box.getString("box_token"));
			}
        }
		
	}
	
	/**
	 * 获取门禁列表
	 * @param cookieStore cookie值
	 * @param url
	 */
	public static Map<Integer,List<Integer>> screen(CookieStore cookieStore,String url) throws Exception{
		System.out.println("Start GET /system/screen 获取门禁列表 ...");
		
		//自定义HttpClient 设置CookieStore
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        HttpGet request = new HttpGet(url);
        
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
        
        //解析JSON数据
        JSONObject resp = new JSONObject(responseBody);
        int result = resp.optInt("code", -1);
        if (result != 0) {
            System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
        }else{
        	JSONArray array = resp.getJSONArray("data");
        	Map<Integer,List<Integer>> screens = new HashMap<Integer, List<Integer>>();
        	for (int i = 0; i < array.length(); i++) {
				JSONObject screen = (JSONObject)array.get(i);
				Integer id = screen.getInt("id");
				List<Integer> allowed_subject_ids = new ArrayList<Integer>();
				JSONArray allowed_subject_ids_array = screen.getJSONArray("allowed_subject_ids");
				for (int j = 0; j < allowed_subject_ids_array.length(); j++) {
					allowed_subject_ids.add((Integer)allowed_subject_ids_array.get(j));
				}
				screens.put(id, allowed_subject_ids);
				System.out.println("id:" + id + ",camera_position:" + screen.getString("camera_position"));
			}
        	return screens;
        }
        return null;
	}
	
	
	/**
	 * 创建一个门禁
	 * @param cookieStore cookie值
	 * @param url
	 * @param Integer box_id 主机ID
	 * @param camera_position 相机位置
	 * @param camera_address 相机视频流地址
	 * @throws Exception
	 */
	public static void addScreen(CookieStore cookieStore ,String url , Integer box_id, String camera_position , String camera_address) throws Exception{
		System.out.println("Start POST /system/screen 创建一个门禁 ...");
		
		//自定义HttpClient 设置CookieStore
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        HttpPost request = new HttpPost(url);
        //设置Content-Type
        request.setHeader("Content-Type", "application/json");
        
        JSONObject json = new JSONObject();
        json.put("box_id", box_id);
        json.put("camera_position", camera_position);
        json.put("camera_address", camera_address);

        request.setEntity(new StringEntity(json.toString(), "UTF-8"));
        
    	CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
        
        //解析JSON数据
        try {
        	JSONObject resp = new JSONObject(responseBody);
            int result = resp.optInt("code", -1);
            if (result != 0) {
                System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
            }else{
            	JSONObject screen = resp.getJSONObject("data");
            	System.out.println("id:" + screen.getInt("id") + ",camera_position:"+ screen.getString("camera_position"));	
            }
		} catch (Exception e) {
			System.err.println(responseBody);
		}
        
	}
	
	
	
	/**
	 * 更新门禁信息
	 * @param cookieStore cookie值
	 * @param url
	 * @param allow_all_subjects 是否允许所有人进入
	 * @param allow_visitor 是否允许访客进入
	 * @param allowed_subject_ids 可进入人员ID列表
	 * @throws Exception
	 */
	public static void updateScreen(CookieStore cookieStore,String url, Boolean allow_all_subjects , Boolean allow_visitor,List<Integer> allowed_subject_ids) throws Exception{
		System.out.println("Start PUT /system/screen/[id] 更新门禁信息 ...");
		
		//自定义HttpClient 设置CookieStore
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        HttpPut request = new HttpPut(url);
        //设置Content-Type
        request.setHeader("Content-Type", "application/json");
        
        JSONObject json = new JSONObject();
        json.put("allow_all_subjects", allow_all_subjects);
        json.put("allow_visitor", allow_visitor);
        json.put("allowed_subject_ids", allowed_subject_ids);
        
        request.setEntity(new StringEntity(json.toString(), "UTF-8"));
        
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
        
        //解析JSON数据
        JSONObject resp = new JSONObject(responseBody);
        int result = resp.optInt("code", -1);
        if (result != 0) {
            System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
        }else{
        	JSONObject screen = resp.getJSONObject("data");
        	System.out.println("id:" + screen.getInt("id") + ",camera_position:"+ screen.getString("camera_position"));	
        }
	}
	
	/**
	 * 获取门禁信息
	 * @param cookieStore cookie值
	 * @param url /system/screen/[id]
	 * @throws Exception
	 */
	public static void getScreen(CookieStore cookieStore,String url) throws Exception{
		System.out.println("Start GET /system/screen/[id] 获取门禁信息 ...");
		
		//自定义HttpClient 设置CookieStore
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        HttpGet request = new HttpGet(url);
        
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
        
        //解析JSON数据
        JSONObject resp = new JSONObject(responseBody);
        int result = resp.optInt("code", -1);
        if (result != 0) {
            System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
        }else{
        	JSONObject screen = resp.getJSONObject("data");
			System.out.println("id:" + screen.getInt("id") + ",camera_position:" + screen.getString("camera_position"));
        }
	}
	
	/**
	 * 删除门禁
	 * @param cookieStore cookie值
	 * @param url /system/screen/[id]
	 * @throws Exception
	 */
	public static void deleteScreen(CookieStore cookieStore,String url) throws Exception{
		System.out.println("Start DELETE /system/screen/[id] 删除门禁 ...");
		
		//自定义HttpClient 设置CookieStore
        CloseableHttpClient httpclient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
        HttpDelete request = new HttpDelete(url);
        
        CloseableHttpResponse response = httpclient.execute(request);
        String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
        
        //解析JSON数据
        JSONObject resp = new JSONObject(responseBody);
        int result = resp.optInt("code", -1);
        if (result != 0) {
            System.err.println("code:" + result + ",desc:" + resp.getString("desc"));
        }else{
        	JSONArray array = resp.getJSONArray("data");
        	for (int i = 0; i < array.length(); i++) {
				JSONObject screen = (JSONObject)array.get(i);
				System.out.println("id:" + screen.getInt("id") + ",camera_position:" + screen.getString("camera_position"));
			}		
        }
	}
	

}
